package com.withoutwords.ui;

import java.util.ArrayList;

import com.withoutwords.R;
import com.withoutwords.engine.Common;
import com.withoutwords.engine.Game;
import com.withoutwords.engine.GameController;
import com.withoutwords.engine.Guess;
import com.withoutwords.engine.Round;
import com.withoutwords.engine.RoundMediaTime;
import com.withoutwords.engine.social.FacebookController;
import com.withoutwords.engine.social.ISocialNetworkController;
import com.withoutwords.engine.social.SocialEngine;
import com.withoutwords.engine.social.SocialNetwork;
import com.withoutwords.ui.custom.GuessesBoardLayout;
import com.withoutwords.ui.custom.IWordsLayoutListener;
import com.withoutwords.ui.custom.WordsLayout;
import com.withoutwords.ui.drawing.BaseDrawingController;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;

import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class BasePlayinyActivity extends BaseActivity implements
		OnClickListener, IWordsLayoutListener {

	protected BaseDrawingController mDrawingController;
	int selectedWordIndex = 0;
	// ArrayList<Guess> UnreadGuesses;
	boolean showAllWords = false;
	// Mode mode;
	protected Round round = null;
	Handler uiHandler;
	protected Game game;
	//
	ISocialNetworkController facebookController;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		round = (Round) getIntent().getSerializableExtra(Common.roundCode);
		if (round == null) {
			if(game == null)
				{game = (Game) getIntent().getSerializableExtra("game");
				if(game != null)
					GameController.getInstance().setCurrentGame(game.getGameId());
				}
			//String gameId = "";
			if (game == null) {
				game =GameController.getInstance().getCurrentGame();
			}
			
			if (game != null)
				round = game.getCurrentRound();
		}
		playSound(R.raw.sound_play_screen_drums);
		// UnreadGuesses = new ArrayList<Guess>();
		updateGuessesNumber();
		uiHandler = new Handler();

	}

	int getMediaTimeResource(RoundMediaTime time) {
		int resID = 0;
		switch (time) {
		}
		return resID;
	}

	/*
	 * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO
	 * Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK) {
	 * showLeaveDialog();
	 * 
	 * } return super.onKeyDown(keyCode, event); }
	 */

	@Override
	protected void onResume() {
		Game currentGame = GameController.getInstance().getCurrentGame();
		if (currentGame != null) {
			Round r = currentGame.getCurrentRound();

			if (round != null) {

				if (r != null && round.equals(r))
					round = r;
				if (round.isRoundSolved()) {
					Game game = GameController.getInstance().getCurrentGame();
					if (game != null)
						GameController.getInstance()
								.leaveGame(game.getGameId());
					finish();
				}

			}

		}

		// TODO Auto-generated method stub
		super.onResume();

	}

	protected void showGuesses() {
		/*
		 * GuessesBoardLayout gb =
		 * (GuessesBoardLayout)findViewById(R.id.guessBoardLayout); if(gb !=
		 * null) { gb.setBackgroundBitmap(); gb.setVisibility(View.VISIBLE); }
		 */

		// updateGuessesNumber();
		Intent intent = new Intent(this, GuesssActivity.class);

		// intent.putExtra(Common.unreadGuessList, UnreadGuesses);
		startActivity(intent);
	}

	protected void initViews() {

		// imgView.setBackgroundResource(getMediaTypeResource(round.getMediaType()));
		ImageButton btn = (ImageButton) findViewById(R.id.btnShowPlayers);
		btn.setOnClickListener(this);
		initDrawingViews();
		initMediaInfoViews(true);
	}

	protected abstract void initDrawingViews();

	/*
	 * { FrameLayout fly = (FrameLayout) findViewById(R.id.canvas_container_id);
	 * 
	 * // fly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, //
	 * LayoutParams.WRAP_CONTENT)); mDrawingView.initializeViews(fly);
	 * 
	 * fly.addView(mCanvasView); // fly.addView(mSettingView); // return fly; }
	 */

	static int id = 0x7f06001f;

	// Returns a valid id that isn't in use
	public int findId() {
		View v = findViewById(id);
		while (v != null) {
			v = findViewById(++id);
		}
		return id++;
	}

	protected void initMediaInfoViews(boolean bEnabled) {
		if (round != null) {
			ImageView imgView = (ImageView) findViewById(R.id.imgViewMediaInfo);
			imgView.setImageResource(round.getMediaInfoResource());
		}
		if (round != null && round.getWords() != null) {
			WordsLayout wly = (WordsLayout) findViewById(R.id.text_container_id);
			wly.createViews(round.getWords(),
					(int) Common.adjustSize(800, this), showAllWords);
			wly.setListener(this);
			wly.setSelectedWord(round.getCurrentWordIndex());

			wly.setEnabled(bEnabled);
			for (int i = 0; i < round.getWordsState().length; i++) {
				if (round.getWordsState()[i]) {
					wly.showWord(i);
				}
			}
		}
	}

	protected void highlightSelectedWord(int wordIndex) {
		WordsLayout wly = (WordsLayout) findViewById(R.id.text_container_id);
		wly.setSelectedWord(wordIndex);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnShowPlayers:
			startActivity(new Intent(this, GamePlayersActivity.class));
			break;

		default:
			break;
		}

	}

	protected void updateGuessesNumber() {

		int nUnreadGuessesNo = 0;
		if (round != null) {
			ArrayList<Guess> UnreadGuesses = round.getUnreadGuesses();
			if (UnreadGuesses != null)
				nUnreadGuessesNo = UnreadGuesses.size();
		}
		// View v = findViewById(R.id.txtGuessesNo);
		TextView txtView = (TextView) findViewById(R.id.txtUnreadGuessNo);
		if (txtView != null) {
			if (nUnreadGuessesNo > 0) {
				txtView.setVisibility(View.VISIBLE);
				txtView.setText(String.valueOf(nUnreadGuessesNo));
			} else {
				txtView.setVisibility(View.INVISIBLE);
				txtView.setText("");
			}
		}
	}

	@Override
	public void onNewDataRecieved(final Intent intent) {
		super.onNewDataRecieved(intent);
		uiHandler.post(new Runnable() {

			@Override
			public void run() {
				Guess g = (Guess) intent.getSerializableExtra("guess");
				if (g != null) {
					
					updateGuessesNumber();
					playSound(R.raw.sound_guess_drums);
				}

			}
		});

	}

	@Override
	public void onBackPressed() {
		Common.showLeaveDialog(this, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Game game = GameController.getInstance().getCurrentGame();
				if (game != null)
					GameController.getInstance().leaveGame(game.getGameId());
				finish();

			}
		}, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();

			}
		});
		// super.onBackPressed();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// MenuItem item1 = Menu.
		menu.add(getString(R.string.share));
		
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	protected abstract  byte[] getDrawingBitmapData() ;
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		
		SocialEngine.getSocialEngine().shareDrawing(this, getString(R.string.share),getDrawingBitmapData());
				return super.onOptionsItemSelected(item);
	}

	
}
